package org.lonetree.tc.core;

import org.lonetree.tc.core.id.ValidatedIdentifier;

/**
 * Business class representing electronic retailers who are sources for books in
 * the system.
 * 
 * All interfaces between different system components should use objects of this
 * class to pass information about electronic sources.
 */
public class ElectronicSource extends Source{

	private Url url;
	
	/**
	 * Create a source for parentBook, with name, selling for $price, with
	 * description, sold at url.
	 *
	 * @param parentBook - the book this source is for
	 * @param name - the name of the online retailer selling the book
	 * @param price - the price the source is selling the book for
	 * @param description - information about the book or source
	 * @param url - the URL where the book can be found for sale
	 */
	public ElectronicSource(Book parentBook, String name, float price, String description, Url url){
		this(null, parentBook, name, price, description, url);
	}
	
	protected ElectronicSource(SourceId sourceId, Book parentBook, String name, float price, String description, Url url){
		super(sourceId, parentBook, name, price, description);

		if(url == null)
			throw new NullPointerException("url cannot be null");
		
		this.url = url;
	}
	
	/**
	 * Get the URL where the book can be found for sale
	 *
	 * @return Url
	 */
	public Url getUrl(){
		return url;
	}

	/**
	 * ValidatedIdentifier representing a URL. While Urls are not used to
	 * uniquely identify any business objects, they do uniquely identify an
	 * address on the internet.
	 */
	public static class Url extends ValidatedIdentifier{
		
		/**
		 * Create a Url encapsulating the passed internet address.
		 *
		 * @param url - the String representing an internet address
		 * @throws ValidationException if the url contains characters other than
		 * 							   letters, numbers, '.', '/', '`', '?',
		 * 							   '&', '%', '=', '$', '{', and '}'.
		 */
		public Url(String url) throws ValidationException{
			super(url);
		}

		@Override
		protected String validateAndFix(String identifier) throws ValidationException{
			// IMPLEMENT Url.validate()
			return identifier;
			
		}
		
		/**
		 * Get the String representation of the URL.
		 *
		 * @return URL
		 */
		public String getUrlString(){
			return getIdentifierString();
		}
	}
}
